package com.itheima.health.service.impl;

import com.itheima.health.dao.OrderSettingDao;
import com.itheima.health.pojo.OrderSetting;
import com.itheima.health.service.OrderSettingService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @author spt
 * @description
 * @date 2019/9/29
 **/
@Service
@Slf4j
public class OrderSettingServiceImpl implements OrderSettingService {
    @Autowired
    private OrderSettingDao orderSettingDao;


    @Override
    public void update(OrderSetting orderSetting) {
        orderSettingDao.update(orderSetting);
    }

    @Override
    public OrderSetting selectByDate(Date orderDate) {
        return orderSettingDao.selectByDate(orderDate);
    }

    @Override
    public void save(OrderSetting orderSetting) {
        orderSettingDao.save(orderSetting);
    }

    /**
     * excel文件导入(批量添加）
     * @param orderSettings
     */
    @Override
    public void add(List<OrderSetting> orderSettings) {
        if (orderSettings != null && orderSettings.size() >0){
            for (OrderSetting orderSetting : orderSettings) {
                //检查此日期是否存在
                long count = orderSettingDao.findCountByDate(orderSetting.getOrderDate());
                if (count > 0){
                    //次日期已经存在
                    orderSettingDao.editNumByOrderDate(orderSetting);
                }
                //日期不存在
                orderSettingDao.save(orderSetting);
            }
        }

    }
    //根据月份查询当月的预约设置
    public List<Map> getOrderSettingByMonth(String date) {
        String dateBegin = date + "-1";//2022-8-1
        String dateEnd = date + "-31";//2022-8-31
        Map map = new HashMap();
        map.put("dateBegin",dateBegin);
        map.put("dateEnd",dateEnd);
        List<OrderSetting> list = orderSettingDao.getOrderSettingByMonth(map);
        List<Map> data = new ArrayList<>();
        for (OrderSetting orderSetting : list) {
            Map orderSettingMap = new HashMap();
            orderSettingMap.put("date",orderSetting.getOrderDate().getDate());//获得日期（几号）
            orderSettingMap.put("number",orderSetting.getNumber());//可预约人数
            orderSettingMap.put("reservations",orderSetting.getReservations());//已预约人数
            data.add(orderSettingMap);
        }
        return data;
    }
}
